/********************************************************************************
* File Name:	addowner.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "addowner.h"
#include "ui_addowner.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include "databaseconnection.h"

addowner::addowner(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::addowner)
{
    ui->setupUi(this);
}

addowner::~addowner()
{
    delete ui;
}

void addowner::on_btnreset_clicked()
{
    this->close();
}

void addowner::on_btnOk_clicked()
{
    if(ui->leoid->text().trimmed()==""||ui->lecontract->text().trimmed()==""||ui->lemoney->text().trimmed()=="")
    {
         QMessageBox::about(this,"Message","item with * can't not be empty!");
         return ;
    }
    QString renterId = ui->leoid->text().trimmed();
    QString renterName = ui->lename->text().trimmed();
    QString contact = ui->lecontact->text().trimmed();
    QString contractId = ui->lecontract->text().trimmed();
    QString price = ui->lemoney->text().trimmed();
    QString remark = ui->lenote->text().trimmed();


    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
    }
    else
    {
        QSqlQuery query;
        query.exec("select count(*) from house.renter where renterId='"+renterId+"'");
        int renterNum=0;
        if(query.next())
        {
            renterNum = query.value(0).toInt();
        }
        if(renterNum!=0)
        {
            QMessageBox::about(this,"Message","this renterid existed!");
            QString maxNum ;
            query.exec("select  renterId from house.renter order by renterId desc");
            if(query.next())
            {
                maxNum = query.value(0).toString();
            }
            QMessageBox::about(this,"Message","max renterId="+maxNum);
            databaseconnection::Close();
            this->close();
            return ;
        }

        //QString cmd = "insert into house.roomInfo values('"+roomId+"','"+renterId+"','"+type+"','"+location+"','"+floor+"','"+ratingNum+"','"+realNum+"','"+area+"','"+price+"','"+air+"','"+phone+"','"+tv+"','"+washer+"','"+internet+"','"+fridge+"','"+remark+"')";
        query.exec("insert into house.renter values('"+renterId+"','"+renterName+"','"+contact+"','"+contractId+"','"+price+"','"+remark+"')");
        if(query.isActive())
        {
            query.numRowsAffected();
            QMessageBox::about(this,"message","add success");

        }
        databaseconnection::Close();
        this->close();
    }
}
